package com.treasure.mall.biz.entity.order;

import com.treasure.mall.biz.entity.BasePO;
import com.treasure.mall.biz.entity.embeddable.GoodsInfo;
import com.treasure.mall.biz.entity.user.MerchantPO;
import com.treasure.mall.biz.entity.user.UserPO;
import com.treasure.mall.zbq.common.ienum.order.OrderEnums;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import java.math.BigDecimal;

/**
 * 售后单
 *
 * @author allan
 * @version 1.0.0
 * @date 2020/6/11
 */
@Entity
@Table(name = "zb_after_sale_bill")
@Setter
@Getter
public class AfterSaleBillPO extends BasePO {
    @Id
    @Column(name = "id", length = 30)
    private String id;
    @Column(name = "sub_order_id")
    private String subOrderId;
    /**
     * 所属商家
     */
    @ManyToOne
    @JoinColumn(name = "rel_merchant_id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
    private MerchantPO merchantPO;
    @Column(name = "order_item_id")
    private Long orderItemId;
    private GoodsInfo goodsInfo;
    @Column(name = "pay_type")
    private OrderEnums.PayType payType;
    /**
     * 申请的用户
     */
    @ManyToOne
    @JoinColumn(name = "rel_user_id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
    private UserPO userPO;
    @Column(name = "after_sale_status")
    private OrderEnums.AfterSaleStatus afterSaleStatus;
    @Column(name = "after_sale_type")
    private OrderEnums.AfterSaleType afterSaleType;
    /**
     * 应退款金额
     */
    @Column(name = "total_amount", scale = 2, precision = 10)
    private BigDecimal totalAmount;
    /**
     * 实际退款金额
     */
    @Column(name = "final_amount", scale = 2, precision = 10)
    private BigDecimal finalAmount;
    /**
     * 退款的余额
     */
    @Column(name = "refund_balance", scale = 2, precision = 10)
    private BigDecimal refundBalance;
    /**
     * 退还一级佣金
     */
    @Column(name = "father_rebate")
    private BigDecimal fatherRebate = BigDecimal.ZERO;
    /**
     * 退还二级佣金
     */
    @Column(name = "grand_rebate")
    private BigDecimal grandRebate = BigDecimal.ZERO;
    /**
     * 退还货款
     */
    @Column(name = "payment")
    private BigDecimal payment = BigDecimal.ZERO;
    @Column(name = "reason")
    private String reason;
    @Column(name = "user_memo")
    private String userMemo;
    @Column(name = "remark")
    private String remark;
    @Column(name = "images")
    private String images;
    @Column(name = "refund_type")
    private OrderEnums.RefundType refundType;

    public String[] images() {
        if (StringUtils.isEmpty(images)) {
            return new String[]{};
        }
        return this.images.split(",");
    }

    public boolean enableRefund() {
        return this.afterSaleStatus == OrderEnums.AfterSaleStatus.TO_REFUND || this.afterSaleStatus == OrderEnums.AfterSaleStatus.WAIT_BUYER_DELIVER_GOODS;
    }
}
